package com.elitel.frame.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.component.RedisCacheComponent;
import com.elitel.frame.base.component.RedisCacheDataBase;
import com.elitel.frame.base.component.RedisCacheProjectService;
import com.elitel.frame.base.service.CfgEditService;
import com.elitel.frame.main.dao.CfgEditMapper;
import com.elitel.frame.main.dao.SysPrjserviceMapper;
import com.elitel.frame.main.dao.ext.ExtBaseEditconfigMapper;
import com.elitel.frame.main.dao.ext.ExtCfgEditMapper;
import com.elitel.frame.main.entity.CfgEdit;
import com.elitel.frame.main.entity.CfgField;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.CfgEditExt;

/**
 * @Description 单表编辑服务(基本信息配置)接口实现类
 * @author guoyanfei
 * @date 2018/01/04
 */
@Service
public class CfgEditServiceImpl implements CfgEditService {
	@Autowired
	private CfgEditMapper cfgEditMapper;
	@Autowired
	private SysPrjserviceMapper sysPrjserviceMapper;
	@Autowired
	private ExtCfgEditMapper extCfgEditMapper;
	@Autowired
	private RedisCacheComponent redisCacheComponent;
	@Autowired
	private RedisCacheProjectService redisCacheProjectService;
	@Autowired
	private ExtBaseEditconfigMapper extBaseEditconfigMapper;
	@Autowired
	private RedisCacheDataBase redisCacheDataBase;
	
	@Override
	public Integer insertCfgEdit(CfgEditExt cfgEditExt) {
		Integer result = 0;
		//新增单表编辑服务基本信息
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CfgEdit cfgEdit = new CfgEdit();
		cfgEdit.setGuid(cfgEditExt.getGuid());
		cfgEdit.setServicename(cfgEditExt.getServicename());
		cfgEdit.setDbKey(cfgEditExt.getDbKey());
		cfgEdit.setDtConfigId(cfgEditExt.getDtConfigId());
		cfgEdit.setRemark(cfgEditExt.getRemark());
		cfgEdit.setCreatetime(new Date());
		result = cfgEditMapper.insertSelective(cfgEdit);
		
		//添加工程服务
		SysPrjservice sysPrjservice = new SysPrjservice();
		sysPrjservice.setGuid(UUID.randomUUID().toString());
		sysPrjservice.setPcode(cfgEditExt.getPcode());
		sysPrjservice.setServicetype(cfgEditExt.getServicetype());
		sysPrjservice.setServicecode(cfgEditExt.getGuid());
		sysPrjservice.setCreatedate(new Date());
		result += sysPrjserviceMapper.insertSelective(sysPrjservice);
		
		//刷新单表编辑服务基本信息Redis缓存
		redisCacheComponent.refreshCfgEditRedisCache();
		
		return result;
	}

	@Override
	public Integer updateCfgEdit(CfgEdit cfgEdit) {
		Integer result = 0;
		//检查修改操作是否修改表名称，如修改，则需删除已配置的校验列
		CfgEdit cfgEditold = cfgEditMapper.selectByPrimaryKey(cfgEdit.getGuid());
		if(!cfgEdit.getDtConfigId().equals(cfgEditold.getDtConfigId())){
			extBaseEditconfigMapper.deleteBaseEidtByserviceId(cfgEdit.getGuid());
		}
		result = cfgEditMapper.updateByPrimaryKeySelective(cfgEdit); 
		
		//刷新单表编辑服务基本信息Redis缓存
		redisCacheComponent.refreshCfgEditRedisCache();
		return result; 
	}

	@Override
	public CfgEdit selectCfgEditbyguid(String guid) {
		return cfgEditMapper.selectByPrimaryKey(guid);
	}

	@Override
	public List<CfgField> selectFieldByguid(String guid) {
		return extCfgEditMapper.selectFieldListByguid(guid);
	}

	@Override
	public List<CfgEdit> selectAllCfgEdit() {
		return extCfgEditMapper.selectAllCfgEdit();
	}

	@Override
	public void deleteCfgEditByPrjservice(SysPrjservice prjservice) {
		//删除服务中间表
		sysPrjserviceMapper.deleteByPrimaryKey(prjservice.getGuid());
		redisCacheProjectService.freshProjectServerTree();
		
		//删除单表编辑服务基本信息表
		cfgEditMapper.deleteByPrimaryKey(prjservice.getServicecode());
		//刷新单表编辑服务基本信息Redis缓存
		redisCacheComponent.refreshCfgEditRedisCache();
		
		//删除配置信息表
		extBaseEditconfigMapper.deleteBaseEidtByserviceId(prjservice.getServicecode());
		//刷新缓存
		redisCacheDataBase.refreshBaseEditconfigRedisCache(prjservice.getServicecode());
	}
	
	

}
